**************************
* 1. PREPARING DATATSETS *
**************************

***************************************************************
* CONTENT:	(1) Merging selected QOG variables to WVS dataset *
*			(2) Merging selected WVS variables to QOG dataset *
***************************************æ***********************

* (1) Merging selected QOG variabes to WVS dataset
**************************************************

* Loading QOG dataset

use qog_std_ts_jan21.dta, clear

* Renaming country variable

gen country=ccode 

* Keeping variables of interest

keep wdi_pop wdi_popf wdi_pop14 wdi_pop65 wdi_pop1564 vdem_libdem vdem_corr wdi_gdpcappppcur wdi_tele wdi_fdiin lp_lat_abst wdi_popurb lp_catho80 lp_muslim80 lp_protmg80 ht_region year country

* Some variables are missig in 2020. 
* So, need to duplicate the value of 2019 to fill these missing values

expand 2 if year==2019, gen(dupindicator) // Duplicate values of 2019
replace year=2020 if dupindicator==1 // Making these duplicates year=2020
replace lp_lat_abst=. if dupindicator==1 // Removing duplicates for which we have actual data for 2020
replace lp_catho80=. if dupindicator==1
replace lp_muslim80=. if dupindicator==1
replace lp_protmg80=. if dupindicator==1
replace ht_region=. if dupindicator==1

bysort country: egen wdi_pop2=max(wdi_pop) if year==2020 // Creating a new variable that captures the duplicate of 2019 for the missing values, and the actual value for the non-missing value
bysort country: egen wdi_popf2=max(wdi_popf) if year==2020
bysort country: egen wdi_pop142=max(wdi_pop14) if year==2020
bysort country: egen wdi_pop652=max(wdi_pop65) if year==2020
bysort country: egen vdem_libdem2=max(vdem_libdem) if year==2020
bysort country: egen vdem_corr2=max(vdem_corr) if year==2020
bysort country: egen wdi_gdpcappppcur2=max(wdi_gdpcappppcur) if year==2020
bysort country: egen wdi_tele2=max(wdi_tele) if year==2020
bysort country: egen wdi_fdiin2=max(wdi_fdiin) if year==2020
bysort country: egen lp_lat_abst2=max(lp_lat_abst) if year==2020
bysort country: egen wdi_popurb2=max(wdi_popurb) if year==2020
bysort country: egen lp_catho802=max(lp_catho80) if year==2020
bysort country: egen lp_muslim802=max(lp_muslim80) if year==2020
bysort country: egen lp_protmg802=max(lp_protmg80) if year==2020
bysort country: egen ht_region2=max(ht_region) if year==2020

drop if dupindicator==1 // Removing the duplicated year

replace wdi_pop=wdi_pop2 if year==2020 // Changing the name of the newly recreate variable
replace wdi_popf=wdi_popf2 if year==2020
replace wdi_pop14=wdi_pop142 if year==2020
replace wdi_pop65=wdi_pop652 if year==2020
replace vdem_libdem=vdem_libdem2 if year==2020
replace vdem_corr=vdem_corr2 if year==2020
replace wdi_gdpcappppcur=wdi_gdpcappppcur2 if year==2020
replace wdi_tele=wdi_tele2 if year==2020
replace wdi_fdiin=wdi_fdiin2 if year==2020
replace lp_lat_abst=lp_lat_abst2 if year==2020
replace wdi_popurb=wdi_popurb2 if year==2020
replace lp_catho80=lp_catho802 if year==2020
replace lp_muslim80=lp_muslim802 if year==2020
replace lp_protmg80=lp_protmg802 if year==2020
replace ht_region=ht_region2 if year==2020

drop wdi_pop2 wdi_popf2 wdi_pop142 wdi_pop652 vdem_libdem2 vdem_corr2 wdi_gdpcappppcur2 wdi_tele2 wdi_fdiin2 lp_lat_abst2 wdi_popurb2 lp_catho802 lp_muslim802 lp_protmg802 ht_region2 dupindicator // Dropping uneccesary variables

* Sorting and saving

sort country year 
save QOG_reduced.dta, replace

* Loading WVS dataset

use WVS_TimeSeries_stata_v1_6.dta, clear

* Renaming country and year variables

gen year=S020 
gen country=S003 

* Sorting and merging

sort country year 
merge m:1 country year using QOG_reduced.dta, nogen

* Creating survey ID variable and ID within-survey variable 

egen survey_id=group(S002 S003)
bysort survey_id: gen group_id=1 if _n==1

* Cleaning outcome variables (E114, E117, D059)

gen E114_clean=E114 if E114>0 // Removing don't knows
replace E114_clean=abs(E114_clean-5) // Reverting the scale
replace E114_clean=. if S003==704 & S002==4 // Vietnman Wave 4 weird translation
replace E114_clean=. if S003==364 & S002==4 // Iran Wave 4 weird translation
replace E114_clean=. if S003==364 & S002==5 // Iran Wave 5 weird translation
replace E114_clean=. if S003==356 // India all waves weird translation
replace E114_clean=. if S003==586 & S002==3 // Pakistan Wave 3 weird translation
replace E114_clean=. if S003==586 & S002==4 // Pakistan Wave 4 weird translation
replace E114_clean=. if S003==417 & S002==5 // Kyrgyzstan Wave 5 weird translation
replace E114_clean=. if S003==417 & S002==6 // Kyrgyzstan Wave 6 weird translation
replace E114_clean=. if S003==642 & S002==4 // Romania Wave 4 weird translation
replace E114_clean=. if S003==642 & S002==5 // Romania Wave 5 weird translation
replace E114_clean=. if S003==642 & S002==6 // Romania Wave 6 weird translation
replace E114_clean=. if S003==818 & S002==6 // Egypt Wave 6 weird translation

gen E117_clean=E117 if E117>0 // Removing don't knows
replace E117_clean=abs(E117_clean-5) // Reverting the scale

gen D059_clean=D059 if D059>0 // Removing don't knows
replace D059_clean=abs(D059_clean-5) // Reverting the scale

* Cleaning socio-demographic variables (X001, X023, X003R, X049, X047)

gen female=0 if X001==1 // Gender variable
replace female=1 if X001==2

gen education=1 if X023<=12 // Education variable, uniformising the X023 and X023R verison of the variable (both versions have been used int he WVS)
replace education=2 if X023==13
replace education=3 if X023==14
replace education=3 if X023==14
replace education=4 if X023==15
replace education=4 if X023==14.5
replace education=5 if X023==16
replace education=6 if X023==17
replace education=7 if X023==18
replace education=8 if X023==19
replace education=9 if X023==20
replace education=10 if X023>=21
replace education=. if X023==.
replace education=X023R if X023R!=.

recode X003R (-5/-1=.) (13/14=.) (98/99=6), gen(age) // Age

recode X049 (-5/-1=.), gen(urban) // Urban

recode X047 (-5/-1=.), gen(income) // Income

* Adding year squared variable and logging variables that need to be logged (population and GDP)

gen year_sq=year^2
gen wdi_pop_log=log(wdi_pop)
gen gdp_log=log(wdi_gdpcappppcur)

* Creating inclusion variables, i.e. variable that take 1 if the respondent has values for all required variables
* Inclusion is different for each dependent variable

gen E114_inclusion=1
replace E114_inclusion=0 if E114_clean==.
replace E114_inclusion=0 if year>2020
replace E114_inclusion=0 if year<1994
replace E114_inclusion=0 if wdi_pop_log==.
replace E114_inclusion=0 if wdi_pop14==. 
replace E114_inclusion=0 if wdi_pop65==.
replace E114_inclusion=0 if wdi_popf==.
replace E114_inclusion=0 if wdi_popurb==.
replace E114_inclusion=0 if vdem_libdem==.
replace E114_inclusion=0 if vdem_corr==.
replace E114_inclusion=0 if gdp_log==.
replace E114_inclusion=0 if wdi_tele==.
replace E114_inclusion=0 if lp_lat_abst==.
replace E114_inclusion=0 if lp_catho80==.
replace E114_inclusion=0 if lp_muslim80==.
replace E114_inclusion=0 if lp_protmg80==.
replace E114_inclusion=0 if female==.
replace E114_inclusion=0 if education==.
replace E114_inclusion=0 if age==.
replace E114_inclusion=0 if urban==.
replace E114_inclusion=0 if income==.

gen E117_inclusion=1
replace E117_inclusion=0 if E117_clean==.
replace E117_inclusion=0 if year>2020
replace E117_inclusion=0 if year<1994
replace E117_inclusion=0 if wdi_pop_log==.
replace E117_inclusion=0 if wdi_pop14==. 
replace E117_inclusion=0 if wdi_pop65==.
replace E117_inclusion=0 if wdi_popf==.
replace E117_inclusion=0 if wdi_popurb==.
replace E117_inclusion=0 if vdem_libdem==.
replace E117_inclusion=0 if vdem_corr==.
replace E117_inclusion=0 if gdp_log==.
replace E117_inclusion=0 if wdi_tele==.
replace E117_inclusion=0 if lp_lat_abst==.
replace E117_inclusion=0 if lp_catho80==.
replace E117_inclusion=0 if lp_muslim80==.
replace E117_inclusion=0 if lp_protmg80==.
replace E117_inclusion=0 if female==.
replace E117_inclusion=0 if education==.
replace E117_inclusion=0 if age==.
replace E117_inclusion=0 if urban==.
replace E117_inclusion=0 if income==.

gen D059_inclusion=1
replace D059_inclusion=0 if D059_clean==.
replace D059_inclusion=0 if year>2020
replace D059_inclusion=0 if year<1994
replace D059_inclusion=0 if wdi_pop_log==.
replace D059_inclusion=0 if wdi_pop14==. 
replace D059_inclusion=0 if wdi_pop65==.
replace D059_inclusion=0 if wdi_popf==.
replace D059_inclusion=0 if wdi_popurb==.
replace D059_inclusion=0 if vdem_libdem==.
replace D059_inclusion=0 if vdem_corr==.
replace D059_inclusion=0 if gdp_log==.
replace D059_inclusion=0 if wdi_tele==.
replace D059_inclusion=0 if lp_lat_abst==.
replace D059_inclusion=0 if lp_catho80==.
replace D059_inclusion=0 if lp_muslim80==.
replace D059_inclusion=0 if lp_protmg80==.
replace D059_inclusion=0 if female==.
replace D059_inclusion=0 if education==.
replace D059_inclusion=0 if age==.
replace D059_inclusion=0 if urban==.
replace D059_inclusion=0 if income==.

* Adding labels to countries

lab define COUNTRY 4 "Afghanistan" 8 "Albania" 12 "Algeria" 20 "Andorra" 24 "Angola" 28 "Antigua and Barbuda" 32 "Argentina" ///
51 "Armenia" 36 "Australia" 40 "Austria" 31 "Azerbaijan" 44 "Bahamas" 48 "Bahrain" 50 "Bangladesh" 52 "Barbados" 112 "Belarus" ///
56 "Belgium" 84 "Belize" 204 "Benin" 64 "Bhutan" 68 "Bolivia" 70 "Bosnia and Herzegovina" 72 "Botswana" 76 "Brazil" 96 "Brunei" ///
100 "Bulgaria" 854 "Burkina Faso" 108 "Burundi" 116 "Cambodia" 120 "Cameroon" 124 "Canada" 132 "Cape Verde" 140 "Central African Republic" ///
148 "Chad" 152 "Chile" 156 "China" 170 "Colombia" 174 "Comoros" 178 "Congo" 180 "Congo, Democratic Republic" 188 "Costa Rica" ///
384 "Cote d'Ivoire" 191 "Croatia" 192 "Cuba" 993 "Cyprus (-1974)" 196 "Cyprus (1975-)" 203 "Czech Republic" 200 "Czechoslovakia" ///
208 "Denmark" 262 "Djibouti" 212 "Dominica" 214 "Dominican Republic" 218 "Ecuador" 818 "Egypt" 222 "El Salvador" 226 "Equatorial Guinea" ///
232 "Eritrea" 233 "Estonia" 230 "Ethiopia (-1992)" 231 "Ethiopia (1993-)" 242 "Fiji" 246 "Finland" 991 "France (-1962)" 250 "France (1963-)" ///
266 "Gabon" 270 "Gambia" 268 "Georgia" 276 "Germany"  278 "Germany, East" 280 "Germany, West" 288 "Ghana" 826 "Great Britain" ///
300 "Greece" 308 "Grenada" 320 "Guatemala" 324 "Guinea" 624 "Guinea-Bissau" 328 "Guyana" 332 "Haiti" 340 "Honduras" 348 "Hungary" ///
352 "Iceland" 356 "India" 360 "Indonesia" 364 "Iran" 368 "Iraq" 372 "Ireland" 376 "Israel" 380 "Italy" 388 "Jamaica" 392 "Japan" ///
400 "Jordan" 398 "Kazakhstan" 404 "Kenya" 296 "Kiribati" 408 "Korea, North" 410 "Korea, South" 414 "Kuwait" 417 "Kyrgyzstan" ///
418 "Laos" 428 "Latvia" 422 "Lebanon" 426 "Lesotho" 430 "Liberia" 434 "Libya" 438 "Liechtenstein" 440 "Lithuania" 442 "Luxembourg" ///
807 "Macedonia" 450 "Madagascar" 454 "Malawi" 458 "Malaysia" 992 "Malaysia (-1965)"  462 "Maldives" 466 "Mali" 470 "Malta" ///
584 "Marshall Islands" 478 "Mauritania" 480 "Mauritius" 484 "Mexico" 583 "Micronesia" 498 "Moldova" 492 "Monaco" 496 "Mongolia" ///
499 "Montenegro" 504 "Morocco" 508 "Mozambique" 104 "Myanmar" 516 "Namibia" 520 "Nauru" 524 "Nepal" 528 "Netherlands" 554 "New Zealand" ///
558 "Nicaragua" 562 "Niger" 566 "Nigeria" 578 "Norway" 512 "Oman" 586 "Pakistan (1971-)" 997 "Pakistan (-1970)" 585 "Palau" ///
591 "Panama" 598 "Papua New Guinea" 600 "Paraguay" 604 "Peru" 608 "Philippines" 616 "Poland" 620 "Portugal" 634 "Qatar" 642 "Romania" ///
643 "Russia" 646 "Rwanda" 882 "Samoa" 674 "San Marino" 678 "Sao Tome and Principe" 682 "Saudi Arabia" 686 "Senegal" 688 "Serbia" ///
891 "Serbia and Montenegro" 690 "Seychelles" 694 "Sierra Leone" 702 "Singapore" 703 "Slovakia" 705 "Slovenia" 90 "Solomon Islands" ///
706 "Somalia" 710 "South Africa" 728 "South Sudan" 724 "Spain" 144 "Sri Lanka" 659 "St Kitts and Nevis" 662 "St Lucia" ///
670 "St Vincent and the Grenadines" 736 "Sudan (-2011)" 729 "Sudan (2012-)" 740 "Suriname" 748 "Swaziland" 752 "Sweden" 756 "Switzerland" ///
760 "Syria" 158 "Taiwan" 762 "Tajikistan" 834 "Tanzania" 764 "Thailand" 994 "Tibet" 626 "Timor-Leste" 768 "Togo" 776 "Tonga" ///
780 "Trinidad and Tobago" 788 "Tunisia" 792 "Turkey" 795 "Turkmenistan" 798 "Tuvalu" 810 "USSR" 800 "Uganda" 804 "Ukraine" ///
784 "United Arab Emirates" 840 "United States" 858 "Uruguay" 860 "Uzbekistan" 548 "Vanuatu" 862 "Venezuela" 704 "Vietnam"  ///
999 "Vietnam, South" 998 "Vietnam, North" 887 "Yemen" 886 "Yemen, North" 720 "Yemen, South" 890 "Yugoslavia" 894 "Zambia" ///
716 "Zimbabwe" 914 "Bosnia" 630 " Puerto Rico" 344 "Hong Kong" 275 "Palestine", modify

lab value country COUNTRY 

* Saving

save WPO_WVS_QOG.dta, replace

* (2) Merging selected WVS variables to QOG dataset
***************************************************

* Loading WVS dataset

use WVS_TimeSeries_stata_v1_6.dta, clear

* Creating survey ID variable and ID within-survey variable 

egen survey_id=group(S002 S003)
bysort survey_id: gen group_id=1 if _n==1

* Cleaning outcome and calculating their mean and sd

gen E114_clean=E114 if E114>0
replace E114_clean=abs(E114_clean-5)
replace E114_clean=. if S003==704 & S002==4 // Vietnman Wave 4
replace E114_clean=. if S003==364 & S002==4 // Iran Wave 4
replace E114_clean=. if S003==364 & S002==5 // Iran Wave 5
replace E114_clean=. if S003==356 // India all waves
replace E114_clean=. if S003==586 & S002==3 // Pakistan Wave 3
replace E114_clean=. if S003==586 & S002==4 // Pakistan Wave 4
replace E114_clean=. if S003==417 & S002==5 // Kyrgyzstan Wave 5
replace E114_clean=. if S003==417 & S002==6 // Kyrgyzstan Wave 6
replace E114_clean=. if S003==642 & S002==4 // Romania Wave 4
replace E114_clean=. if S003==642 & S002==5 // Romania Wave 5
replace E114_clean=. if S003==642 & S002==6 // Romania Wave 6
replace E114_clean=. if S003==818 & S002==6 // Egypt Wave 6
bysort survey_id: egen E114_mean_wvs=mean(E114_clean)

gen E117_clean=E117 if E117>0
replace E117_clean=abs(E117_clean-5)
bysort survey_id: egen E117_mean_wvs=mean(E117_clean)

gen D059_clean=D059 if D059>0
replace D059_clean=abs(D059_clean-5)
bysort survey_id: egen D059_mean_wvs=mean(D059_clean)

* Cleaning socio-demographic variables and calculating their mean and sd

gen female=0 if X001==1
replace female=1 if X001==2

egen female_mean=mean(female), by(survey_id)
egen female_sd = sd(female), by(survey_id)

gen education=1 if X023<=12
replace education=2 if X023==13
replace education=3 if X023==14
replace education=3 if X023==14
replace education=4 if X023==15
replace education=4 if X023==14.5
replace education=5 if X023==16
replace education=6 if X023==17
replace education=7 if X023==18
replace education=8 if X023==19
replace education=9 if X023==20
replace education=10 if X023>=21
replace education=. if X023==.
replace education=X023R if X023R!=.
egen edu_mean=mean(education), by(survey_id)
egen edu_sd = sd(education), by(survey_id)

recode X003R (-5/-1=.) (13/14=.) (98/99=6), gen(age)
egen age_mean=mean(age), by(survey_id)
egen age_sd = sd(age), by(survey_id)

recode X049 (-5/-1=.), gen(urban)
egen urban_mean=mean(urban), by(survey_id)
egen urban_sd = sd(urban), by(survey_id)

recode X047 (-5/-1=.), gen(income)
egen income_mean=mean(income), by(survey_id)
egen income_sd = sd(income), by(survey_id)

* Calculating correlations between socio-demographic variables
 
egen corr_female_edu=corr(female education), by(survey_id)
egen corr_female_age=corr(female age), by(survey_id)
egen corr_female_urban=corr(female urban), by(survey_id)
egen corr_female_income=corr(female income), by(survey_id)
egen corr_edu_age=corr(education age), by(survey_id)
egen corr_edu_urban=corr(education urban), by(survey_id)
egen corr_edu_income=corr(education income), by(survey_id)
egen corr_age_urban=corr(age urban), by(survey_id)
egen corr_age_income=corr(age income), by(survey_id)
egen corr_urban_income=corr(urban income), by(survey_id)

* Taking one observation per survey

keep if group_id==1 

* Renaming year and country variables

gen year=S020 
gen country=S003 

* Keeping the variables of interest

keep E114_mean_wvs E117_mean_wvs D059_mean_wvs edu_mean edu_sd age_mean age_sd female_mean female_sd urban_mean urban_sd income_mean income_sd corr_female_edu corr_female_age corr_female_urban corr_female_income corr_edu_age corr_edu_urban corr_edu_income corr_age_urban corr_age_income corr_urban_income country year survey_id group_id

* Sorting and saving

sort country year
save WVS_reduced.dta, replace

* Loading QOG datasett

use qog_std_ts_jan21.dta, clear

* Renaming country variable

gen country=ccode 

* Duplicating 2019 variables to fill in missing 2020 variables (same as above)

expand 2 if year==2019, gen(dupindicator)
replace year=2020 if dupindicator==1

replace lp_lat_abst=. if dupindicator==1
replace lp_catho80=. if dupindicator==1
replace lp_muslim80=. if dupindicator==1
replace lp_protmg80=. if dupindicator==1
replace ht_region=. if dupindicator==1

bysort country: egen wdi_pop2=max(wdi_pop) if year==2020
bysort country: egen wdi_popf2=max(wdi_popf) if year==2020
bysort country: egen wdi_pop142=max(wdi_pop14) if year==2020
bysort country: egen wdi_pop15642=max(wdi_pop1564) if year==2020
bysort country: egen wdi_pop652=max(wdi_pop65) if year==2020
bysort country: egen vdem_libdem2=max(vdem_libdem) if year==2020
bysort country: egen vdem_corr2=max(vdem_corr) if year==2020
bysort country: egen wdi_gdpcappppcur2=max(wdi_gdpcappppcur) if year==2020
bysort country: egen wdi_tele2=max(wdi_tele) if year==2020
bysort country: egen wdi_fdiin2=max(wdi_fdiin) if year==2020
bysort country: egen lp_lat_abst2=max(lp_lat_abst) if year==2020
bysort country: egen wdi_poprul2=max(wdi_poprul) if year==2020
bysort country: egen wdi_popurb2=max(wdi_popurb) if year==2020
bysort country: egen lp_catho802=max(lp_catho80) if year==2020
bysort country: egen lp_muslim802=max(lp_muslim80) if year==2020
bysort country: egen lp_protmg802=max(lp_protmg80) if year==2020
bysort country: egen ht_region2=max(ht_region) if year==2020

drop if dupindicator==1

replace wdi_pop=wdi_pop2 if year==2020
replace wdi_popf=wdi_popf2 if year==2020
replace wdi_pop14=wdi_pop142 if year==2020
replace wdi_pop1564=wdi_pop15642 if year==2020
replace wdi_pop65=wdi_pop652 if year==2020
replace vdem_libdem=vdem_libdem2 if year==2020
replace vdem_corr=vdem_corr2 if year==2020
replace wdi_gdpcappppcur=wdi_gdpcappppcur2 if year==2020
replace wdi_tele=wdi_tele2 if year==2020
replace wdi_fdiin=wdi_fdiin2 if year==2020
replace lp_lat_abst=lp_lat_abst2 if year==2020
replace wdi_poprul=wdi_poprul2 if year==2020
replace wdi_popurb=wdi_popurb2 if year==2020
replace lp_catho80=lp_catho802 if year==2020
replace lp_muslim80=lp_muslim802 if year==2020
replace lp_protmg80=lp_protmg802 if year==2020
replace ht_region=ht_region2 if year==2020

drop wdi_pop2 wdi_popf2 wdi_pop142 wdi_pop15642 wdi_pop652 vdem_libdem2 vdem_corr2 wdi_gdpcappppcur2 wdi_tele2 wdi_fdiin2 lp_lat_abst2 wdi_poprul2 wdi_popurb2 lp_catho802 lp_muslim802 lp_protmg802 ht_region2 dupindicator

* Merging with WVS dataset

merge 1:m country year using WVS_reduced, nogen

* Saving

save WPO_QOG_WVS.dta, replace

* Adding UN membership variable

gen UN=0
replace UN=1 if ccode==4 & year>=1946 // Afghanistan
replace UN=1 if ccode==8 & year>=1955 // Albania
replace UN=1 if ccode==12 & year>=1962 // Algeria
replace UN=1 if ccode==20 & year>=1993 // Andorra
replace UN=1 if ccode==24 & year>=1976 // Angola
replace UN=1 if ccode==28 & year>=1981 // Antigua and Barbuda
replace UN=1 if ccode==31 & year>=1992 // Azerbaijan
replace UN=1 if ccode==32 & year>=1945 // Argentina
replace UN=1 if ccode==36 & year>=1945 // Australia
replace UN=1 if ccode==40 & year>=1955 // Austria
replace UN=1 if ccode==44 & year>=1973 // Bahamas
replace UN=1 if ccode==48 & year>=1971 // Bahrain
replace UN=1 if ccode==50 & year>=1974 // Bangladesh
replace UN=1 if ccode==51 & year>=1992 // Armenia
replace UN=1 if ccode==52 & year>=1966 // Barbados
replace UN=1 if ccode==56 & year>=1945 // Belgium
replace UN=1 if ccode==64 & year>=1971 // Bhutan
replace UN=1 if ccode==68 & year>=1945 // Bolivia
replace UN=1 if ccode==70 & year>=1992 // Bosnia and Herzegovina
replace UN=1 if ccode==72 & year>=1966 // Botswana =20
replace UN=1 if ccode==76 & year>=1945 // Brazil
replace UN=1 if ccode==84 & year>=1981 // Belize
replace UN=1 if ccode==90 & year>=1978 // Solomon Islands
replace UN=1 if ccode==96 & year>=1984 // Brunei
replace UN=1 if ccode==100 & year>=1955 // Bulgaria
replace UN=1 if ccode==104 & year>=1948 // Myanmar/Burma
replace UN=1 if ccode==108 & year>=1962 // Burundi
replace UN=1 if ccode==112 & year>=1945 // Belarus
replace UN=1 if ccode==116 & year>=1955 // Cambodia
replace UN=1 if ccode==120 & year>=1960 // Cameroon
replace UN=1 if ccode==124 & year>=1945 // Canada
replace UN=1 if ccode==132 & year>=1975 // Cabo Verde
replace UN=1 if ccode==140 & year>=1960 // Central African Republic
replace UN=1 if ccode==144 & year>=1955 // Sri Lanka
replace UN=1 if ccode==148 & year>=1960 // Chad
replace UN=1 if ccode==152 & year>=1945 // Chile
replace UN=1 if ccode==156 & year>=1945 // China
replace UN=1 if ccode==170 & year>=1945 // Colombia
replace UN=1 if ccode==174 & year>=1975 // Comoros
replace UN=1 if ccode==178 & year>=1960 // Congo =40
replace UN=1 if ccode==180 & year>=1960 // Democratic Republic of Congo
replace UN=1 if ccode==188 & year>=1945 // Costa Rica
replace UN=1 if ccode==191 & year>=1992 // Croatia
replace UN=1 if ccode==192 & year>=1945 // Cuba
replace UN=1 if ccode==196 & year>=1960 // Cyprus (1975-)
replace UN=1 if ccode==203 & year>=1993 // Czech Republic
replace UN=1 if ccode==204 & year>=1960 // Benin
replace UN=1 if ccode==208 & year>=1945 // Denmark
replace UN=1 if ccode==212 & year>=1978 // Dominica
replace UN=1 if ccode==214 & year>=1945 // Dominican Republic
replace UN=1 if ccode==218 & year>=1945 // Ecuador
replace UN=1 if ccode==222 & year>=1945 // El Salvador
replace UN=1 if ccode==226 & year>=1968 // Equatorial Guinea
replace UN=1 if ccode==230 & year>=1945 // Ethiopia (-1992)
replace UN=1 if ccode==231 & year>=1945 // Ethiopia (1993-)
replace UN=1 if ccode==232 & year>=1993 // Eritrea
replace UN=1 if ccode==233 & year>=1991 // Estonia
replace UN=1 if ccode==242 & year>=1970 // Fiji
replace UN=1 if ccode==246 & year>=1955 // Finland
replace UN=1 if ccode==250 & year>=1945 // France =60
replace UN=1 if ccode==262 & year>=1977 // Djibouti
replace UN=1 if ccode==266 & year>=1960 // Gabon
replace UN=1 if ccode==268 & year>=1992 // Georgia
replace UN=1 if ccode==270 & year>=1965 // Republic of the Gambia
replace UN=1 if ccode==276 & year>=1973 // Germany (united)
replace UN=1 if ccode==278 & year>=1973 // East Germany
replace UN=1 if ccode==280 & year>=1973 // West Germany
replace UN=1 if ccode==288 & year>=1957 // Ghana
replace UN=1 if ccode==296 & year>=1999 // Kiribati
replace UN=1 if ccode==300 & year>=1945 // Greece
replace UN=1 if ccode==308 & year>=1974 // Grenada
replace UN=1 if ccode==320 & year>=1945 // Guatemala
replace UN=1 if ccode==324 & year>=1958 // Guinea
replace UN=1 if ccode==328 & year>=1966 // Guyana
replace UN=1 if ccode==332 & year>=1945 // Haiti
replace UN=1 if ccode==340 & year>=1945 // Honduras
replace UN=1 if ccode==348 & year>=1955 // Hungary
replace UN=1 if ccode==352 & year>=1946 // Iceland
replace UN=1 if ccode==356 & year>=1945 // India
replace UN=1 if ccode==360 & year>=1950 // Indonesia =80
replace UN=1 if ccode==364 & year>=1945 // Iran
replace UN=1 if ccode==368 & year>=1945 // Iraq
replace UN=1 if ccode==372 & year>=1955 // Ireland
replace UN=1 if ccode==376 & year>=1949 // Israel
replace UN=1 if ccode==380 & year>=1955 // Italy
replace UN=1 if ccode==384 & year>=1960 // Cote d'Ivoire
replace UN=1 if ccode==388 & year>=1962 // Jamaica
replace UN=1 if ccode==392 & year>=1956 // Japan
replace UN=1 if ccode==398 & year>=1992 // Kazakshtan
replace UN=1 if ccode==400 & year>=1955 // Jordan
replace UN=1 if ccode==404 & year>=1963 // Kenya
replace UN=1 if ccode==410 & year>=1991 // South Korea
replace UN=1 if ccode==408 & year>=1991 // North Korea
replace UN=1 if ccode==414 & year>=1963 // Kuwait
replace UN=1 if ccode==417 & year>=1992 // Kyrgyzstan
replace UN=1 if ccode==418 & year>=1955 // Lao People's Democratic Republic
replace UN=1 if ccode==422 & year>=1945 // Lebanon
replace UN=1 if ccode==426 & year>=1966 // Lesotho
replace UN=1 if ccode==428 & year>=1991 // Latvia
replace UN=1 if ccode==430 & year>=1945 // Liberia =100
replace UN=1 if ccode==434 & year>=1955 // Libya
replace UN=1 if ccode==438 & year>=1990 // Liechtenstein
replace UN=1 if ccode==440 & year>=1991 // Lithuania
replace UN=1 if ccode==442 & year>=1945 // Luxembourg
replace UN=1 if ccode==450 & year>=1960 // Madagascar
replace UN=1 if ccode==454 & year>=1964 // Malawi
replace UN=1 if ccode==458 & year>=1957 // Malaysia
replace UN=1 if ccode==462 & year>=1965 // Maldives
replace UN=1 if ccode==466 & year>=1960 // Mali
replace UN=1 if ccode==470 & year>=1964 // Malta
replace UN=1 if ccode==478 & year>=1961 // Mauritania
replace UN=1 if ccode==480 & year>=1968 // Mauritius
replace UN=1 if ccode==484 & year>=1945 // Mexico
replace UN=1 if ccode==492 & year>=1993 // Monaco
replace UN=1 if ccode==496 & year>=1961 // Mongolia
replace UN=1 if ccode==498 & year>=1992 // Moldova
replace UN=1 if ccode==499 & year>=2006 // Montenegro
replace UN=1 if ccode==504 & year>=1956 // Morocco
replace UN=1 if ccode==508 & year>=1975 // Mozambique
replace UN=1 if ccode==512 & year>=1971 // Oman =120
replace UN=1 if ccode==516 & year>=1990 // Namibia
replace UN=1 if ccode==520 & year>=1999 // Nauru
replace UN=1 if ccode==524 & year>=1955 // Nepal
replace UN=1 if ccode==528 & year>=1945 // Netherlands
replace UN=1 if ccode==548 & year>=1981 // Vanuatu
replace UN=1 if ccode==554 & year>=1945 // New Zealand
replace UN=1 if ccode==558 & year>=1945 // Nicaragua
replace UN=1 if ccode==562 & year>=1960 // Niger
replace UN=1 if ccode==566 & year>=1960 // Nigeria
replace UN=1 if ccode==578 & year>=1945 // Norway
replace UN=1 if ccode==583 & year>=1991 // Micronesia
replace UN=1 if ccode==584 & year>=1991 // Marshall Islands
replace UN=1 if ccode==585 & year>=1994 // Palau
replace UN=1 if ccode==586 & year>=1947 // Pakistan (1971-)
replace UN=1 if ccode==591 & year>=1945 // Panama
replace UN=1 if ccode==598 & year>=1975 // Papua New Guinea
replace UN=1 if ccode==600 & year>=1945 // Paraguay
replace UN=1 if ccode==604 & year>=1945 // Peru
replace UN=1 if ccode==608 & year>=1945 // Philippines
replace UN=1 if ccode==616 & year>=1945 // Poland =140
replace UN=1 if ccode==624 & year>=1974 // Guinea-Bissau
replace UN=1 if ccode==626 & year>=2002 // Timor-Leste
replace UN=1 if ccode==634 & year>=1971 // Qatar
replace UN=1 if ccode==642 & year>=1955 // Romania
replace UN=1 if ccode==643 & year>=1945 // Russian Federation
replace UN=1 if ccode==646 & year>=1962 // Rwanda
replace UN=1 if ccode==659 & year>=1983 // St Kitts and Nevis
replace UN=1 if ccode==662 & year>=1979 // St Lucia
replace UN=1 if ccode==670 & year>=1980 // St Vincent and the Grenadines
replace UN=1 if ccode==674 & year>=1992 // San Marino
replace UN=1 if ccode==678 & year>=1975 // Sao Tome and Principe
replace UN=1 if ccode==682 & year>=1945 // Saudi Arabia
replace UN=1 if ccode==686 & year>=1960 // Senegal
replace UN=1 if ccode==688 & year>=2000 // Serbia
replace UN=1 if ccode==690 & year>=1976 // Seychelles
replace UN=1 if ccode==694 & year>=1961 // Sierra Leone
replace UN=1 if ccode==702 & year>=1965 // Singapore
replace UN=1 if ccode==703 & year>=1993 // Slovakia
replace UN=1 if ccode==704 & year>=1977 // Vietnam
replace UN=1 if ccode==705 & year>=1992 // Slovenia =160
replace UN=1 if ccode==706 & year>=1960 // Somalia
replace UN=1 if ccode==710 & year>=1945 // South Africa
replace UN=1 if ccode==716 & year>=1980 // Zimbabwe
replace UN=1 if ccode==720 & year>=1947 // South Yemen
replace UN=1 if ccode==724 & year>=1955 // Spain
replace UN=1 if ccode==728 & year>=2011 // South Sudan
replace UN=1 if ccode==729 & year>=1956 // Sudan (2012-)
replace UN=1 if ccode==736 & year>=1956 // Sudan (-2011)
replace UN=1 if ccode==740 & year>=1975 // Suriname
replace UN=1 if ccode==748 & year>=1968 // Eswatini (Swaziland)
replace UN=1 if ccode==752 & year>=1946 // Sweden
replace UN=1 if ccode==756 & year>=2002 // Switzerland
replace UN=1 if ccode==760 & year>=1945 // Syrian Arab Republic
replace UN=1 if ccode==762 & year>=1992 // Tajikistan
replace UN=1 if ccode==764 & year>=1946 // Thailand
replace UN=1 if ccode==768 & year>=1960 // Togo
replace UN=1 if ccode==776 & year>=1999 // Tonga
replace UN=1 if ccode==780 & year>=1962 // Trinidad and Tobago
replace UN=1 if ccode==784 & year>=1971 // UAE
replace UN=1 if ccode==788 & year>=1956 // Tunisia =180
replace UN=1 if ccode==792 & year>=1945 // Turkey
replace UN=1 if ccode==795 & year>=1992 // Turkmenistan
replace UN=1 if ccode==798 & year>=2000 // Tuvalu
replace UN=1 if ccode==800 & year>=1962 // Uganda
replace UN=1 if ccode==804 & year>=1945 // Ukraine
replace UN=1 if ccode==807 & year>=1993 // North Macedonia
replace UN=1 if ccode==818 & year>=1945 // Egypt
replace UN=1 if ccode==826 & year>=1945 // UK
replace UN=1 if ccode==834 & year>=1961 // United Republic of Tanzania
replace UN=1 if ccode==840 & year>=1945 // USA
replace UN=1 if ccode==854 & year>=1960 // Burkina Faso
replace UN=1 if ccode==858 & year>=1945 // Uruguay
replace UN=1 if ccode==860 & year>=1992 // Uzbekistan
replace UN=1 if ccode==862 & year>=1945 // Bolivarian Republic of Venezuela
replace UN=1 if ccode==882 & year>=1976 // Samoa
replace UN=1 if ccode==886 & year>=1947 // North Yemen
replace UN=1 if ccode==887 & year>=1947 // Yemen (united)
replace UN=1 if ccode==894 & year>=1964 // Zambia
replace UN=1 if ccode==914 & year>=1992 // Bosnia (informal name)
replace UN=1 if ccode==991 & year>=1945 // France (-1962) =200
replace UN=1 if ccode==992 & year>=1957 // Malaysia (-1965)
replace UN=1 if ccode==993 & year>=1960 // Cyprus (-1974)
replace UN=1 if ccode==997 & year>=1947 // Pakistan (-1970)
replace UN=1 if ccode==998 & year>=1977 // North Vietnam
replace UN=1 if ccode==999 & year>=1977 // South Vietnam =205

* Creating inclusion variables for each dependent variable.

gen E114_inclusion=1
replace E114_inclusion=0 if E114_mean_wvs==.

bysort country: egen max_E114=max(E114_mean_wvs)

gen E114_inclusion_country=1
replace E114_inclusion_country=0 if max_E114==.

gen E117_inclusion=1
replace E117_inclusion=0 if E117_mean_wvs==.

bysort country: egen max_E117=max(E117_mean_wvs)

gen E117_inclusion_country=1
replace E117_inclusion_country=0 if max_E117==.
replace E117_inclusion_country=0 if max_E117==.

gen D059_inclusion=1
replace D059_inclusion=0 if D059_mean_wvs==.

bysort country: egen max_D059=max(D059_mean_wvs)

gen D059_inclusion_country=1
replace D059_inclusion_country=0 if max_D059==.

* Saving

save WPO_QOG_WVS.dta, replace
